const router = require('koa-router')()
const mysql = require('mysql');
const { query } = require('../public/javascripts/db')

router.prefix('/record')

router.post('/calendar', async (ctx, next) => {
    let options = { 'ok': false };
    let { ym, day, userId } = ctx.request.body;
    let sql = 'select * from review where user_id=? and ym=?';
    let sql1 = 'select * from reviewdc where user_id=? and ym=?';
    let sql2 = 'select * from reviewdc where user_id=? and days=?';
    await query(sql, [userId, ym]).then((results) => { options.ok = true; options.monthMsg = results });
    await query(sql1, [userId, ym]).then((results) => { options.ok = true; options.allDays = results });
    await query(sql2, [userId, day]).then((results) => { options.ok = true; options.dayMsg = results });
    ctx.body = options;
})

router.post('/submit', async (ctx, next) => {
    let options = { 'ok': false };
    let { MorD, ym, content, day, userId } = ctx.request.body;
    if (MorD == 'month') {
        let sql = 'insert into review(ym,month_content,user_id) value(?,?,?)';
        await query(sql, [ym, content, userId]).then(options.ok = true);
    } else {
        let sql = 'insert into reviewdc(ym,content,days,user_id) value(?,?,?,?)';
        await query(sql, [ym, content, day, userId]).then(options.ok = true);
    }
    ctx.body = options;
})

module.exports = router
